<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="编码"></el-input>
    </el-form-item>
    <el-form-item label="产地地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="产地地址"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="手机号码" prop="mobilePhone">
      <el-input v-model="dataForm.mobilePhone" placeholder="手机号码"></el-input>
    </el-form-item>
    <el-form-item label="许可证编号" prop="permitNumber">
      <el-input v-model="dataForm.permitNumber" placeholder="许可证编号"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-radio-group v-model="dataForm.status">
        <el-radio label="1">启用</el-radio>
        <el-radio label="0">停用</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="备注" prop="memo">
      <el-input v-model="dataForm.memo" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="ischeck">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {manufacturerGetObj, manufacturerAddObj, manufacturerPutObj} from '@/api/eims/manufacturer'

    export default {
    data () {
      return {
        visible: false,
        ischeck: false,
        dataForm: {
          id: 0,
          name: '',
          code: '',
          address: '',
          phone: '',
          mobilePhone: '',
          permitNumber: '',
          status: '1',
          memo: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          tenantId: '',
          orgId: '',
          unitId: '',
          delFlag: ''
        },
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '产地地址不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            manufacturerGetObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                manufacturerPutObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                  this.ischeck = false
                }).catch(() => {
                  this.ischeck = false
                })
            } else {
                manufacturerAddObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                  this.ischeck = false
                }).catch(() => {
                  this.ischeck = false
                })
            }
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
